//Will Zepeda
//GSP-362
//Text.cpp
#include "Text.h"
#include "DirectX.h"
#include "Definitions.h"

Text::Text()
{
	mFont = NULL;
	mSize = BUF_SIZE;
	mText = new char[mSize];

	mRect.bottom	= SCREEN_HEIGHT;
	mRect.right		= SCREEN_WIDTH;
	mRect.left		= 0;
	mRect.top		= 0;

	mColor = D3DCOLOR_RGBA(255,255,255,255);

	HRESULT hr = D3DXCreateFont(DirectXFramework::Instance()->getDevice(), 0, 0, 0, 0, false, 
		DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, DEFAULT_QUALITY, 
		DEFAULT_PITCH | FF_DONTCARE, TEXT("Arial"), &mFont);
	// Check if the Font was Created //
	if(hr != S_OK)
	{
		::MessageBoxA(::HWND(), "D3DXCreateFont has failed in Text.cpp", "Mayan Maze", MB_OK | MB_ICONERROR);
	}
	setText(" ");
}
Text::Text(char * fontType)
{
	mFont = NULL;
	mSize = BUF_SIZE;
	mText = new char[mSize];

	mRect.bottom	= SCREEN_HEIGHT;
	mRect.right		= SCREEN_WIDTH;
	mRect.left		= 0;
	mRect.top		= 0;

	mColor = D3DCOLOR_RGBA(255,255,255,255);

	HRESULT hr = D3DXCreateFont(DirectXFramework::Instance()->getDevice(), 0, 0, 0, 0, false, 
		DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, DEFAULT_QUALITY, 
		DEFAULT_PITCH | FF_DONTCARE, fontType, &mFont);
	// Check if the Font was Created //
	if(hr != S_OK)
	{
		::MessageBoxA(::HWND(), "D3DXCreateFont has failed in Text.cpp", "Mayan Maze", MB_OK | MB_ICONERROR);
	}
	setText(" ");
}
Text::Text(int aHeight, bool makeBold, bool makeItalic, char* fontType)
{
	mFont = NULL;
	mSize = BUF_SIZE;
	mText = new char[mSize];

	mRect.bottom	= SCREEN_HEIGHT;
	mRect.right		= SCREEN_WIDTH;
	mRect.left		= 0;
	mRect.top		= 0;

	mColor = D3DCOLOR_RGBA(255,255,255,255);

	HRESULT hr;
	if(!makeBold)// if you don't want to make font bold, weight param == FW_NORMAL
	{
		hr = D3DXCreateFont(DirectXFramework::Instance()->getDevice(),
			aHeight, 0, FW_NORMAL, 0, makeItalic, 
			DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, 
			DEFAULT_QUALITY, DEFAULT_PITCH | FF_DONTCARE,
			fontType, &mFont);
	}
	else		// bold, weight param == FW_BOLD 
	{
		hr = D3DXCreateFont(DirectXFramework::Instance()->getDevice(),
			aHeight, 0, FW_BOLD, 0, makeItalic, 
			DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, 
			DEFAULT_QUALITY, DEFAULT_PITCH | FF_DONTCARE,
			fontType, &mFont);
	}
	if(hr != S_OK)
	{
		::MessageBoxA(::HWND(), "D3DXCreateFont has failed in Text.cpp", "Mayan Maze", MB_OK | MB_ICONERROR);
	}
	setText(" ");
}
Text::~Text()
{
	if(mText != NULL)
	{
		delete [] mText;
		mText = NULL;
	}
	if(mFont != NULL)
	{
		mFont->Release();
		mFont = NULL;
	}
}
void Text::allocateBufferSize(char * aText, int aCount)
{
	//allocates array to size of how many characters were passed in
	char * newBuf = new char[aCount+1];//+1 is for null terminator
	//copy old data into new array
	//NOTE: does not make mText a null terminated char buffer
	for(int i = 0; i < mSize; ++i)
	{
		newBuf[i] = mText[i];
	}
	//explicitly add null terminator because DrawText needs a null terminated string
	newBuf[aCount] = '\0';
	//get rid of old data
	delete [] mText;
	//make mText point to new data
	mText = newBuf;
	mSize = aCount+1;
}
void Text::checkArgSize(char * aText)
{
	int count = 0;
	//reads in char * and counts up how many characters were passed in
	for(int i = 0; aText[i] != '\0'; ++i)
	{
		count++;
	}
	if(count > mSize)
	{
		allocateBufferSize(aText, count);
	}
}
void Text::setPosition(int x, int y)
{
	//setPosition(left, top)
	SetRect(&mRect, x, y, SCREEN_WIDTH, SCREEN_HEIGHT);
}
void Text::setPosAndSize(int left, int top, int right, int bottom)
{
	//setPosition(left, top, right, bottom)
	SetRect(&mRect, left, top, right, bottom);
}
void Text::setText(char * aText)
{
	//checks to see if aText's size is bigger than mSize
	//if true it calls allocateBufferSize and explicity changes mText
	checkArgSize(aText);
	wsprintf(mText, aText);
}
void Text::setColor(int r, int g, int b, int a)
{
	mColor = D3DCOLOR_RGBA(r,g,b,a);
}
void Text::update()
{
	//this is where i would change the colors
}
void Text::render()
{
	//This time our Right and Bottom coordinates in the rect are important. 
	//They'll be used as the boundaries for wrapping and clipping. 
	//Setting the DT_WORDBREAK flag makes it wrap sentences at word breaks. 
	//DT_LEFT|DT_WORDBREAK

	/**
	int DrawText(
	  __in     HDC hDC,
	  __inout  LPCTSTR lpchText,
	  __in     int nCount, # of chars you want to render. -1 needs \0 string
	  __inout  LPRECT lpRect,
	  __in     UINT uFormat
	);
	**/ 
	//returns zero if function fails
	if(!mFont->DrawText(NULL, mText, -1, &mRect, 
		DT_LEFT|DT_TOP|DT_WORDBREAK, mColor))
	{
		::MessageBox(::HWND(), "DrawText() failed in Text.cpp", "Mayan Maze", 0); 
	}
}
void Text::renderCentered()
{
	//returns zero if function fails
	if(!mFont->DrawText(NULL, mText, -1, &mRect, 
		DT_CENTER|DT_VCENTER|DT_WORDBREAK, mColor))
	{
		::MessageBox(::HWND(), "DrawText() failed in Text.cpp", "Mayan Maze", 0); 
	}
}